package com.technology.education.ui.student.popularize

import androidx.core.os.bundleOf
import com.angcyo.dsladapter.*
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.AppData
import com.technology.education.data.model.Record
import com.technology.education.dslitem.ItemSingleText
import com.technology.education.ext.*
import me.hgj.jetpackmvvm.ext.parseState

/**
 * 学生推广中心
 * 底部列表展示积分收入信息
 */
class PopularizeFragment : BaseRecyclerTopBarFragment<PopularizeViewModel>() {
    override fun initViews() {
        setTitle("推广中心")
    }

    override fun lazyLoadData() {
        loading()
        setUI()
    }

    private fun setUI() {
        renderAdapter {
            updateOrInsertHeaderItem<DslAdapterItem>("code", 0) {
                it.apply {
                    itemLayoutId = R.layout.item_invitated_code
                    itemBindOverride = { itemHolder, _, _, _ ->
                        itemHolder.tv(R.id.tv_code)?.text =
                            getAppViewModel().userInfo.value?.inviteCode
                        itemHolder.click(R.id.iv_copy) {
                            (getAppViewModel().userInfo.value?.inviteCode ?: "").copyToClip(
                                requireContext()
                            )
                            toast("邀请码已复制到剪切板")
                        }
                    }
                }
            }
            updateOrInsertHeaderItem<DslAdapterItem>("time", 1) {
                it.apply {
                    itemLayoutId = R.layout.item_popularize_socre_student
                    itemBindOverride = { itemHolder, _, _, _ ->
                        itemHolder.tv(R.id.tv_score_remain)?.text =
                            getAppViewModel().userInfo.value?.balancePoints.toString()
                        itemHolder.click(R.id.tv_convert_time) { view ->
                            ///兑换时长
                            navigation(
                                view, R.id.action_popularizeFragment_to_withdrawFragment,
                                bundleOf("type" to 5)
                            )
                        }
                        itemHolder.click(R.id.tv_rule){
                            toWebActivity("推广规则",url = AppData.POPULARIZE_PROTOCOL)
                        }
                        itemHolder.click(R.id.tv_convert_goods) { view ->
                            //兑换商品
                            navigation(view, R.id.action_popularizeFragment_to_goodsListFragment)
                        }
                        itemHolder.click(R.id.tv_convert_money) { view ->
                            //兑换现金
                            navigation(
                                view, R.id.action_popularizeFragment_to_withdrawStudentFragment
                            )
                        }
                    }
                }
            }
            updateOrInsertHeaderItem<ItemSingleText>("info", 2) {
                it.apply {
                    title = "获得时长已自动添加至时长余额"
                    style.titleTextColor = R.color.red
                    style.titleTextSize = 14f
                    style.topPadding = 10
                }
            }

            updateOrInsertHeaderItem<DslAdapterItem>("menu", 3) {
                it.apply {
                    itemLayoutId = R.layout.item_popularize_menu_student
                    itemBindOverride = { itemHolder, _, _, _ ->
                        itemHolder.click(R.id.tv_mine) { it ->
                            ///我的推广
                            navigation(
                                it,
                                R.id.action_popularizeFragment_to_popularizeRecordFragment
                            )
                        }
                        itemHolder.click(R.id.tv_record) { it ->
                            navigation(it, R.id.action_popularizeFragment_to_scoreRecordFragment)
                        }
                        itemHolder.click(R.id.tv_invite) {
                            //todo:立即推广
                        }
                    }
                }
            }

            updateOrInsertHeaderItem<ItemSingleText>("title", 4) {
                it.apply {
                    title = "奖励记录"
                    style.isBold = true
                    style.bottomPadding = 0
                }
            }

        }
    }

    override fun createObserver() {
        getAppViewModel().userInfo.observe(this){
            renderAdapter {
                updateAllItem()
            }
        }
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { it ->
                hasMore(it.total)
                renderAdapter {
                    setPageItems(
                        it.list.toDslItemList(R.layout.item_score_record) {
                            itemBindOverride = { itemHolder, _, _, _ ->
                                itemData?.let { it ->
                                    if (it is Record) {
                                        itemHolder.tv(R.id.tv_title)?.text = it.title
                                        itemHolder.tv(R.id.tv_date)?.text = it.createDate
                                        itemHolder.tv(R.id.tv_score)?.text = it.value.toString()
                                    }
                                }
                            }
                        },
                        mViewModel.pageIndex
                    )
                }
            })
        }
    }
}